﻿using StableDiffusionTools.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VAELinker.Entry
{
    public class EntryBase
    {
        public enum FileType
        {
            Unkown,
            Model,
            Vae
        }

        public string RootDirectory { get; protected set; }
        public string FileName { get; protected set; }
        public string PathName { get; protected set; }
        public string FullName { get; protected set; }
        public string FileNameNoExtension { get; protected set; }
        public string Extension { get; protected set; }
        public FileType Type { get; protected set; }
        public string Size { get; protected set; }

        public EntryBase(string fullName, string root, FileType type = FileType.Unkown)
        {
            RootDirectory = root;
            FullName = fullName;
            FileName = Path.GetFileName(fullName);
            FileNameNoExtension = Path.GetFileNameWithoutExtension(FileName);
            Extension = Path.GetExtension(FileName);
            int startIndex = FullName.IndexOf(RootDirectory) + RootDirectory.Length + 1;
            int length = fullName.IndexOf(FileName) - startIndex;
            PathName = FullName.Substring(startIndex, length);
            Type = type;
            Size = $"{FileUtil.ConvertSize(new FileInfo(fullName).Length, "GB"):0.00}GiB";
        }

        public static FileType GetFileType(string fileName)
        {
            if (fileName.EndsWith(".vae.pt", StringComparison.InvariantCultureIgnoreCase) || fileName.EndsWith(".vae.safetensors", StringComparison.InvariantCultureIgnoreCase))
                return FileType.Vae;
            else if (fileName.EndsWith(".ckpt", StringComparison.InvariantCultureIgnoreCase) || fileName.EndsWith(".safetensors", StringComparison.InvariantCultureIgnoreCase))
                return FileType.Model;
            else
                return FileType.Unkown;
        }

        public static EntryBase GetEntry(string fullName, string rootFolder)
        {
            EntryBase result = null;
            FileType type = GetFileType(fullName);
            if(type == FileType.Model)
            {
                result = new EntryModel(fullName, rootFolder);
            } 
            else if(type == FileType.Vae)
            {
                result = new EntryVAE(fullName, rootFolder);
            }
            return result;
        }

        public virtual void Renamed(string fullName)
        {
            FullName = fullName;
            FileName = Path.GetFileName(fullName);
            FileNameNoExtension = Path.GetFileNameWithoutExtension(FileName);
            Extension = Path.GetExtension(FileName);
            int startIndex = FullName.IndexOf(RootDirectory) + RootDirectory.Length + 1;
            int length = fullName.IndexOf(FileName) - startIndex;
            PathName = FullName.Substring(startIndex, length);
            //DisplayName = FullName.Substring(FullName.IndexOf(RootDirectory) + RootDirectory.Length + 1);
        }
    }
}
